package com.roads.engineering.controller;

import com.roads.common.annotation.Log;
import com.roads.common.constant.Constants;
import com.roads.common.core.controller.BaseController;
import com.roads.common.core.domain.AjaxResult;
import com.roads.common.core.page.TableDataInfo;
import com.roads.common.enums.BusinessType;
import com.roads.engineering.domain.ChangeOrderDetail;
import com.roads.engineering.domain.ChangeOrderMaster;
import com.roads.engineering.service.IChangeOrderDetailService;
import com.roads.engineering.service.IChangeOrderMasterService;
import com.roads.project.domain.ProBoqMeterage;
import com.roads.project.domain.ProInstitution;
import com.roads.project.service.IProInstitutionService;
import com.roads.workflow.domain.WorkflowTaskNode;
import com.roads.workflow.service.IWorkflowTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 变更令申报详情Controller
 *
 * @author nhp
 * @date 2022-01-07
 */
@RestController
@RequestMapping("/engineering/change/detail")
public class ChangeOrderDetailController extends BaseController {

    @Autowired
    private IChangeOrderDetailService service;

    @Autowired
    private IChangeOrderMasterService masterService;

    @Autowired
    private IWorkflowTaskService taskService;

    @Autowired
    private IProInstitutionService institutionService;

    /**
     * 查询变更令详情
     */
    @GetMapping("/list")
    public TableDataInfo list(ChangeOrderDetail model) {
        startPage();
        List<ChangeOrderDetail> list = service.selectModelList(model);
        return getDataTable(list);
    }

    /**
     * 获取用户节点列表
     *
     * @param changeId 主键ID
     * @return 结果
     */
    @GetMapping(value = "/selectUserOptions/{changeId}")
    public AjaxResult selectUserOptions(@PathVariable("changeId") Long changeId) {
        AjaxResult result = new AjaxResult();
        ChangeOrderMaster master = masterService.getModelById(changeId);
        List<WorkflowTaskNode> nodes = taskService.selectUserOptions(master.getUuid());
        result.put("currentUser", master.getCurrentUser());
        result.put("nodes", nodes);
        return result;
    }

    /**
     * 查询未选择的计量清单
     */
    @GetMapping("/unallocatedMeterageList")
    public TableDataInfo unallocatedMeterageList(ChangeOrderDetail model) {
        ChangeOrderMaster master = masterService.getModelById(model.getChangeId());
        ProInstitution institution = institutionService.getModelByInnerCode(master.getInstitutionCode());
        if (Constants.INSTITUTION_TYPE_F.equals(institution.getInstitutionType())) {
            // 分包如果共享清单，则取父项标段计量清单
            ProInstitution parent = institutionService.getModelByInnerCode(institution.getParentCode());
            if (Constants.YES.equals(parent.getIsShare())) {
                model.setItem(parent.getInnerCode());
            } else {
                model.setItem(master.getInstitutionCode());
            }
        } else {
            model.setItem(master.getInstitutionCode());
        }
        startPage();
        List<ProBoqMeterage> list = service.unallocatedMeterageList(model);
        return getDataTable(list);
    }

    /**
     * 批量新增变更清单
     */
    @PreAuthorize("@ss.hasPermi('engineering:change:detail:add')")
    @Log(title = "变更令详情", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    public AjaxResult save(Long changeId, String ids) {
        return toAjax(service.save(changeId, ids));
    }

    /**
     * 获取详细信息
     */
    @GetMapping(value = "/{changeDetailId}")
    public AjaxResult getInfo(@PathVariable("changeDetailId") Long changeDetailId) {
        return AjaxResult.success(service.getModelById(changeDetailId));
    }

    /**
     * 修改
     */
    @PreAuthorize("@ss.hasPermi('engineering:change:detail:edit')")
    @Log(title = "变更令详情", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ChangeOrderDetail model) {
        return toAjax(service.update(model));
    }

    /**
     * 删除
     */
    @PreAuthorize("@ss.hasPermi('engineering:change:detail:remove')")
    @Log(title = "变更令详情", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(service.deleteByIds(ids));
    }

}
